<template>
	<view :style="colorStyle" style="min-height: 100vh;background-color: #fff;">
		<view class="mp-header"
			style="background: linear-gradient(to bottom, rgb(254, 242, 240) 0%, rgb(254, 250, 244) 70%, rgb(255, 255, 255) 100%);">
			<view class="sys-head" :style="{ height: sysHeight }"></view>
			<view class="serch-box" style="height: 43px; line-height: 43px;text-align: center;position: relative;">
				<view class="backBtn" @click="pageBack" style="margin-top: 15rpx;">
					<image src="/static/svg/back.svg" style="width: 24px;height: 24px;"></image>
				</view>
				
				<view class="name">优惠券</view>
			</view>
		</view>
		<view class="navbar acea-row row-around" :style="{marginTop: heightNum + 43 + 'px'}">
			<view class="item acea-row row-center-wrapper" :class="{ on: navOn === 1 }" @click="onNav(1)">
				<view class="name" style="position: relative;z-index: 10;">{{$t(`未使用`)}}</view>
			</view>
			<view class="item acea-row row-center-wrapper" :class="{ on: navOn === 2 }" @click="onNav(2)">
				<view class="name" style="position: relative;z-index: 10;">{{$t(`已使用/过期`)}}</view>
			</view>
			<view class="item acea-row row-center-wrapper" :class="{ on: navOn === 3 }" @click="onNav(3)">
				<view class="name" style="position: relative;z-index: 10;">{{$t(`可领取`)}}</view>
			</view>
		</view>
		
		<view v-if="couponsList.length" style="margin-top: 80rpx;">
			<!-- 优惠券内容 -->
			<div class="couponList" v-if="navOn === 1">
				<div v-for="coupon in couponsList" :key="coupon.id" class="nBox">
					<div class="couponCard">
						<div class="couponLeft">
							<!-- <div class="couponType">通用券</div> -->
							<view class="name line2">
								<view class="couponType" :class="coupon._type === 0 ? 'bg-color-huic' : 'bg-color-check'"
									v-if="coupon.applicable_type === 0">{{$t(`通用劵`)}}</view>
								<view class="couponType" :class="coupon._type === 0 ? 'bg-color-huic' : 'bg-color-check'"
									v-else-if="coupon.applicable_type === 1">{{$t(`品类券`)}}</view>
								<view class="couponType" :class="coupon._type === 0 ? 'bg-color-huic' : 'bg-color-check'"
									v-else>{{$t(`商品券`)}}</view>
							</view>
							<div class="couponAmount">
								<div class="unit">￥</div>
								<div class="price">{{ coupon.coupon_price }}</div>
							</div>
						</div>
						<div class="couponRight">
							<p class="couponTitle">{{ coupon.coupon_title }}</p>
							<view class="couponLimit" v-if="coupon.use_min_price > 0">
								{{ $t(`满`) }} {{ coupon.use_min_price }}
								{{ $t(`元可用`) }}
							</view>
							<view class="couponLimit" v-else>{{ $t(`无门槛券`) }}</view>
						</div>
						<div class="couponAction" @click="switchJamp('/pages/goods_cate/goods_cate')">去使用</div>
					</div>
					<div class="couponDate">有效时间：{{ coupon._add_time }} - {{ coupon._end_time }}</div>
				</div>
			</div>
			
			<div class="couponList used" v-if="navOn === 2">
				<div v-for="coupon in couponsList" :key="coupon.id" class="nBox">
					<div class="couponCard">
						<div class="couponLeft">
							<!-- <div class="couponType">通用券</div> -->
							<view class="name line2">
								<view class="couponType" :class="coupon._type === 0 ? 'bg-color-huic' : 'bg-color-check'"
									v-if="coupon.applicable_type === 0">{{$t(`通用劵`)}}</view>
								<view class="couponType" :class="coupon._type === 0 ? 'bg-color-huic' : 'bg-color-check'"
									v-else-if="coupon.applicable_type === 1">{{$t(`品类券`)}}</view>
								<view class="couponType" :class="coupon._type === 0 ? 'bg-color-huic' : 'bg-color-check'"
									v-else>{{$t(`商品券`)}}</view>
							</view>
							<div class="couponAmount">
								<div class="unit">￥</div>
								<div class="price">{{ coupon.coupon_price }}</div>
							</div>
						</div>
						<div class="couponRight">
							<p class="couponTitle">{{ coupon.coupon_title }}</p>
							<view class="couponLimit" v-if="coupon.use_min_price > 0">
								{{ $t(`满`) }} {{ coupon.use_min_price }}
								{{ $t(`元可用`) }}
							</view>
							<view class="couponLimit" v-else>{{ $t(`无门槛券`) }}</view>
						</div>
						<div class="couponAction gray">已使用</div>
					</div>
					<div class="couponDate">有效时间：{{ coupon._add_time }} - {{ coupon._end_time }}</div>
				</div>
			</div>
			<div class="couponList" v-if="navOn === 3">
				<div v-for="coupon in couponsList" :key="coupon.id" class="nBox">
					<div class="couponCard">
						<div class="couponLeft">
							<!-- <div class="couponType">通用券</div> -->
							<view class="name line2">
								<view class="couponType" :class="coupon._type === 0 ? 'bg-color-huic' : 'bg-color-check'"
									v-if="coupon.applicable_type === 0">{{$t(`通用劵`)}}</view>
								<view class="couponType" :class="coupon._type === 0 ? 'bg-color-huic' : 'bg-color-check'"
									v-else-if="coupon.applicable_type === 1">{{$t(`品类券`)}}</view>
								<view class="couponType" :class="coupon._type === 0 ? 'bg-color-huic' : 'bg-color-check'"
									v-else>{{$t(`商品券`)}}</view>
							</view>
							<div class="couponAmount">
								<div class="unit">￥</div>
								<div class="price">{{ coupon.coupon_price }}</div>
							</div>
						</div>
						<div class="couponRight">
							<p class="couponTitle">{{ coupon.title }}</p>
							<view class="couponLimit" v-if="coupon.use_min_price > 0">
								{{ $t(`满`) }} {{ coupon.use_min_price }}
								{{ $t(`元可用`) }}
							</view>
							<view class="couponLimit" v-else>{{ $t(`无门槛券`) }}</view>
						</div>
						<!-- <div class="couponAction" @click="getCoupon(coupon.id, coupon)">领取</div> -->
						
						
						<view class="bnt gray couponAction" v-if="coupon.is_use >= coupon.receive_limit">{{ $t(`已领取`) }}</view>
						<view class="bnt gray couponAction" v-else-if="coupon.is_permanent == 0 && coupon.remain_count == 0">
							{{ $t(`已领完`) }}
						</view>
						<view class="bnt bg-color couponAction" v-else @click="getCoupon(coupon.id, coupon)">{{ $t(`立即领取`) }}</view>
					</div>
					<!-- <div class="couponDate">{{ $t(`领取后`) }} {{ coupon.coupon_time }} {{ $t(`天内可用`) }}</div> -->
					<view class="couponDate" v-if="coupon.coupon_time">{{ $t(`领取后`) }} {{ coupon.coupon_time }} {{ $t(`天内可用`) }}</view>
					<view class="couponDate" v-else-if="coupon.start_use_time || coupon.end_use_time">{{ coupon.start_use_time ? coupon.start_use_time + '-' : '' }}{{ coupon.end_use_time }}</view>
					<view class="couponDate" v-else></view>
				</div>
			</div>
		</view>
		<!-- <view class='coupon-list' v-if="couponsList.length">
			<view class='item acea-row row-center-wrapper' v-for='(item,index) in couponsList' :key="index"
				:class="{svip: item.receive_type === 4}" @click="useCoupon(item)">
				<view class="moneyCon acea-row row-center-wrapper">
					<view class='money' :class='item._type == 0 ? "moneyGray" : ""'>
						<view>{{$t(`￥`)}}<text class='num'>{{item.coupon_price}}</text></view>
						<view class="pic-num" v-if="item.use_min_price > 0">
							{{$t(`满`)}}{{item.use_min_price}}{{$t(`元可用`)}}
						</view>
						<view class="pic-num" v-else>{{$t(`无门槛券`)}}</view>
					</view>
				</view>
				<view class='text'>
					<view class='condition'>
						<view class="name line2">
							<view class="line-title" :class="item._type === 0 ? 'bg-color-huic' : 'bg-color-check'"
								v-if="item.applicable_type === 0">{{$t(`通用劵`)}}</view>
							<view class="line-title" :class="item._type === 0 ? 'bg-color-huic' : 'bg-color-check'"
								v-else-if="item.applicable_type === 1">{{$t(`品类券`)}}</view>
							<view class="line-title" :class="item._type === 0 ? 'bg-color-huic' : 'bg-color-check'"
								v-else>{{$t(`商品券`)}}</view>
							<image src="../../../static/images/fvip.png" class="pic" v-if="item.receive_type===4">
							</image>
							{{$t(item.coupon_title)}}
						</view>
					</view>
					<view class='data acea-row row-between-wrapper'>
						<view>{{item.add_time}}-{{item.end_time}}</view>
						<view class='bnt gray' v-if="item._type==0">{{$t(item._msg)}}</view>
						<view class='bnt bg-color' v-else>{{$t(item._msg)}}</view>
					</view>
				</view>
			</view>
		</view> -->
		<view class='noCommodity' v-if="!couponsList.length && page === 2">
			<view class='pictrue'>
				<!-- <image :src="imgHost + '/statics/images/noCoupon.png'"></image> -->
				<image src="https://shop.hercter.com/uploads/attach/2025/03/20250303/627d5b5503379efe066666a1cfc21866.png" style="height: 400rpx;filter: hue-rotate(21.5deg) saturate(0) contrast(1);opacity: 0.16;" mode="heightFix"></image>
				<view class="noData" style="text-align: center;">暂无优惠券</view>
			</view>
		</view>
		<!-- #ifdef MP -->
		<!-- <authorize @onLoadFun="onLoadFun" :isAuto="isAuto" :isShowAuth="isShowAuth" @authColse="authColse"></authorize> -->
		<!-- #endif -->
		<!-- #ifndef MP -->
		<home></home>
		<!-- #endif -->
	</view>
</template>

<script>
	let heightNum = uni.getSystemInfoSync().statusBarHeight;
	let sysHeight = heightNum + 'px';
	import {
		getUserCoupons
	} from '@/api/api.js';
	import {
		toLogin
	} from '@/libs/login.js';
	import {
		mapGetters
	} from "vuex";
	// #ifdef MP
	import authorize from '@/components/Authorize';
	// #endif
	import home from '@/components/home';
	import colors from '@/mixins/color.js';
	import { getCoupons, setCouponReceive } from '@/api/api.js';
	import {
		HTTP_REQUEST_URL
	} from '@/config/app';
	export default {
		components: {
			// #ifdef MP
			authorize,
			// #endif
			home
		},
		mixins: [colors],
		data() {
			return {
				imgHost: HTTP_REQUEST_URL,
				couponsList: [],
				loading: false,
				isAuto: false, //没有授权的不会自动授权
				isShowAuth: false, //是否隐藏授权
				navOn: 1,
				page: 1,
				limit: 15,
				finished: false,
				sysHeight: sysHeight,
				heightNum: heightNum,
				couldCouponsList: [],
				receiveLoading: false
			};
		},
		computed: mapGetters(['isLogin']),
		watch: {
			isLogin: {
				handler: function(newV, oldV) {
					if (newV) {
						this.getUseCoupons();
					}
				},
				deep: true
			}
		},
		onLoad() {
			if (this.isLogin) {
				// this.getCoupons(-1);
				// this.getCoupons(1);
				this.getUseCoupons();
			} else {
				toLogin();
			}
		},
		onReachBottom() {
			this.getUseCoupons();
		},
		methods: {
			switchJamp(url){
				uni.switchTab({
					url: url
				})
			},
			jumpTo(url){
				uni.navigateTo({
					url: url
				})
			},
			pageBack(){
				uni.navigateBack();
			},
			onNav: function(type) {
				this.navOn = type;
				this.couponsList = [];
				this.page = 1;
				this.finished = false;
				if(type == 3){
					this.getCoupons(-1);
				}
				if(type == 1 || type == 2){
					this.getUseCoupons();
				}
			},
			useCoupon(item) {
				if (this.navOn == 2) return
				let url = '';
				if (item.category_id == 0 && item.product_id == '') {
					url = '/pages/goods/goods_list/index?title=默认'
				}
				if (item.category_id != 0) {
					url = `/pages/goods/goods_list/index?title=${item.coupon_title}&coupon_category_id=${item.category_id}`
				}
				if (item.product_id != '') {
					let arr = item.product_id.split(',');
					let num = arr.length;
					if (num == 1) {
						url = '/pages/goods_details/index?id=' + item.product_id
					} else {
						url = '/pages/goods/goods_list/index?productId=' + item.product_id + '&title=默认'
					}
				}
				uni.navigateTo({
					url: url
				});
			},
			getCoupon: function (id, item) {
				if (this.receiveLoading) return;
				let that = this;
				this.receiveLoading = true;
				//领取优惠券
				setCouponReceive(id)
					.then((res) => {
						item.is_use += 1;
						that.$util.Tips({
							title: that.$t(`领取成功`)
						});
						setTimeout((e) => {
							that.receiveLoading = false;
						}, 500);
					})
					.catch((error) => {
						that.receiveLoading = false;
						return that.$util.Tips({
							title: error
						});
					});
			},
			/**
			 * 获取领取优惠券列表
			 */
			getCoupons: function (type) {
				let that = this;
				getCoupons({
					page: 1,
					limit: 10000,
					type
				}).then((res) => {
					console.log('优惠券', res);
					this.couponsList = res.data.list;
				})
				.catch((err) => {
				});
			},
			/**
			 * 授权回调
			 */
			onLoadFun: function() {
				this.getUseCoupons();
			},
			// 授权关闭
			authColse: function(e) {
				this.isShowAuth = e
			},
			/**
			 * 获取领取优惠券列表
			 */
			getUseCoupons: function() {
				let that = this;
				if (that.loading || that.finished) {
					return;
				}
				that.loading = true;
				uni.showLoading({
					title: that.$t(`正在加载…`)
				});
				getUserCoupons(this.navOn, {
					page: this.page,
					limit: this.limit
				}).then(res => {
					that.loading = false;
					uni.hideLoading();
					that.couponsList = that.couponsList.concat(res.data);
					that.finished = res.data.length < that.limit;
					that.page += 1;
				}).catch(err => {
					that.loading = false;
					uni.showToast({
						title: err,
						icon: 'none'
					});
				});
			}
		}
	}
</script>

<style>
	.money {
		display: flex;
		flex-direction: column;
		justify-content: center;
	}

	.pic-num {
		color: #ffffff;
		font-size: 24rpx;
	}
	.coupon-list .item{
	}
	.coupon-list .item .text{
		padding: 14rpx 10rpx;
	}
	.coupon-list .item .text .condition {
		display: flex;
	}

	.coupon-list .item .text .condition .name {
		font-size: 24rpx;
		font-weight: 500;
		line-height: 44rpx;
		/* display: flex;
		align-items: center; */
	}

	.coupon-list .item .text .condition .pic {
		width: 30rpx;
		height: 30rpx;
		display: block;
		margin-right: 10rpx;
		display: inline-block;
		vertical-align: middle;
	}

	.condition .line-title {
		/* width: 70rpx; */
		height: 36rpx !important;
		padding: 0 5px;
		line-height: 32rpx;
		text-align: center;
		box-sizing: border-box;
		background: rgba(255, 247, 247, 1);
		border: 1px solid var(--view-theme);
		opacity: 1;
		border-radius: 20rpx;
		font-size: 18rpx !important;
		color: var(--view-theme);
		margin-right: 12rpx;
		text-align: center;
		display: inline-block;
		vertical-align: middle;
	}

	.condition .line-title.bg-color-huic {
		border-color: #BBB !important;
		color: #bbb !important;
		background-color: #F5F5F5 !important;
	}
</style>

<style lang="scss" scoped>
	.navbar {
		position: fixed;
		top: 0;
		left: 0;
		width: 100%;
		height: 90rpx;
		background-color: #FFFFFF;
		z-index: 9;
		padding: 0 80rpx;
		box-sizing: border-box;

		.item {
			border-top: 5rpx solid transparent;
			border-bottom: 5rpx solid transparent;
			font-size: 30rpx;
			color: #999999;
			position: relative;
			position: relative;
			z-index: 10;
			
			&.on {
				// border-bottom-color: var(--view-theme);
				color: #282828;
				font-weight: bold;
				&::after{
					content: '';
					width: 100%;
					height: 20rpx;
					background: linear-gradient(to right, rgb(247, 169, 116), rgb(243, 54, 97));
					position: absolute;
					bottom: 15rpx;
					left: 0;
					z-index: 1;
				}
			}
		}
	}

	.coupon-list {
		margin-top: 122rpx;
	}

	.noCommodity {
		margin-top: 300rpx;
	}
	
	.couponList {
	    // display: flex;
	    // gap: 12px;
	    // justify-content: space-between;
		padding: 20px;
		
		&.used{
			.couponAction{
				background-color: #b2b2b2;
				color: #fff;
			}
		}
	}
	
	.nBox{
	    background-color: #fff;
	    border: 1px solid #f6f6f6;
	    // box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
		margin-bottom: 10px;
		background-image: url('http://39.98.108.135:8000/zhx_images/v2_sn4n3e.png');
		background-size: 80% auto;
		background-repeat: no-repeat;
		background-position-x: 60px;
	}
	
	.couponCard {
	    display: flex;
	    width: 100%;
	    position: relative;
	}
	
	.couponLeft {
	    padding: 0 10px;
	    display: flex;
	    flex-direction: column;
	    align-items: center;
	}
	
	.couponAmount {
	    color: #333;
	    margin-top: 10px;
	    display: flex;
	
	    .unit {
	        position: relative;
	        top: 6px;
	        color: #666;
	    }
	
	    .price {
	        font-size: 26px;
	        font-weight: bold;
	    }
	}
	
	.couponType {
	    font-size: 12px;
	    margin-top: 5px;
	    color: #555;
	    background-color: rgb(246, 246, 246);
	    padding: 3px 0;
	    width: 60px;
	    text-align: center;
	}
	
	.couponRight {
	    padding: 25px 15px 10px 15px;
	    flex-grow: 1;
	}
	
	.couponTitle {
	    font-weight: bold;
	    margin-bottom: 5px;
	}
	
	.couponLimit{
	    font-size: 12px;
	    color: #777;
	}
	
	.couponDate {
	    font-size: 12px;
	    color: #777;
	    padding: 5px 15px;
	    margin-bottom: 5px;
	}
	
	.couponAction {
	    position: absolute;
	    right: 0;
	    top: -1px;
	    background: linear-gradient(to right, rgb(247, 167, 116), rgb(243, 55, 97));
	    color: #fff;
	    font-size: 14px;
	    font-weight: bold;
	    height: 193rpx;
		// line-height: 220rpx;
	    padding: 0 10px;
		padding-top: 15rpx;
		box-sizing: border-box;
	    writing-mode: vertical-rl;
	    text-align: center;
	    border-radius: 0;
	    cursor: pointer;
		letter-spacing: 5px;
		border-radius: 0 10rpx 10rpx 0;
		
		&.gray{
			background: #999;
		}
	}
	
	page{
		background-color: #fff;
	}
</style>
